/*
 * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xdlinkdemo.device.provider;

import android.content.Context;
import android.support.annotation.NonNull;

import com.xuexiang.xdlink.XDLink;
import com.xuexiang.xdlink.component.channel.IChannel;
import com.xuexiang.xdlink.component.channel.impl.MockChannel;
import com.xuexiang.xdlink.component.command.IProtocolCommand;
import com.xuexiang.xdlink.component.message.IDataInfo;
import com.xuexiang.xdlink.component.message.IMessage;
import com.xuexiang.xdlink.core.IDeviceConnectCenter;
import com.xuexiang.xdlink.core.impl.AbstractDeviceDataProvider;
import com.xuexiang.xutil.tip.ToastUtils;

/**
 * 模拟设备的数据提供者
 *
 * @author xuexiang
 * @since 2018/8/30 上午11:51
 */
public class MockDeviceDataProvider extends AbstractDeviceDataProvider {

    /**
     * 构造函数
     *
     * @param context
     */
    public MockDeviceDataProvider(Context context) {
        super(context);

        initDevice();
    }

    @Override
    public void onReceivedMessage(IMessage message) {


    }

    @Override
    public void onReceivedDataInfo(IDataInfo dataInfo) {

    }

    @NonNull
    @Override
    public IDeviceConnectCenter buildDeviceConnectCenter(Context context) {
        MockChannel mockChannel = XDLink.mock(context, "mock/test.data")
                .setReadInterval(1000)
                .build();
        return XDLink.newDevice("mock_device")
                .setChannel(mockChannel)
                .setDeviceListeners(this, this)
                .build();
    }

    @Override
    public IProtocolCommand onChannelConnectStatusChanged(IChannel channel) {
        switch(channel.getConnectStatus()) {
            case CONNECT_SUCCESS:
                ToastUtils.toast("开始模拟...");
                break;
            case CONNECT_FAILED:
                ToastUtils.toast("模拟失败!");
                break;
            default:
                break;
        }
        return null;
    }

}
